//
//  ZZMoreViewController.m
//  ZuZuBao
//
//  Created by 龚洪 on 2024/4/25.
//

#import "ZZMoreViewController.h"
#import "ZZHomeHeaderView.h"
#import "ZZHomeTableViewCell.h"
#import "ZZProductDetailViewController.h"

@interface ZZMoreViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, assign) int type;

@end

@implementation ZZMoreViewController

-(instancetype)initWithType:(int)type {
    self = [super init];
    if (self) {
        self.type = type;
        switch (type) {
            case 3:
                self.title = @"游戏手柄";
                break;
            case 8:
                self.title = @"游戏卡带";
                break;
            case 20:
                self.title = @"游戏电脑";
                break;
            default:
                break;
        }
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = [NSMutableArray array];
    [self createView];
    [self getData];
}

-(void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarHeight, kScreenWidth, kScreenHeight - kNavBarHeight) style:UITableViewStylePlain];
    self.tableView.backgroundColor = kBackColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}

-(void)getData {
    NSString *urlString = [NSString stringWithFormat:@"%@%@", kBaseUrl, kQryGoods];
    NSDictionary *params = @{@"current": @(1),
                                 @"gameId" : @(self.type),
                                 @"labelType": @(4),
                                 @"size": @(20),
                                 @"synthesizeSort": @(1)
                                 };
    
    [kFFNetManager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            NSDictionary * reponseDic = responseObject[@"result"];
            [self.dataArray removeAllObjects];
            NSArray * array = reponseDic[@"record"];
            for (int i = 0; i < array.count; i++) {
                NSDictionary * dic = array[i];
                ZZProductModel * model = [[ZZProductModel alloc] init];
                model = [ZZProductModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }

            [self.tableView reloadData];
        }else {
             
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 返回你想要的行数
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 155;
}
  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 注册自定义cell的类，如果还没有注册过
    static NSString *cellIdentifier = @"ZZHomeTableViewCell";
    [tableView registerClass:[ZZHomeTableViewCell class] forCellReuseIdentifier:cellIdentifier];
      
    // 从队列中复用cell或创建新的cell
    ZZHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    // 设置cell的数据
    [cell setCellData:self.dataArray[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZZProductDetailViewController * vc = [[ZZProductDetailViewController alloc] init];
    vc.model = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
